/*
 * Copyright 2011 OpenWAF.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package com.openwaf.client.ui.panels;

import com.openwaf.client.dom.DivElement;
import com.openwaf.client.dom.Element;
import com.openwaf.client.dom.Node;
import com.openwaf.client.dom.SpanElement;
import com.openwaf.client.dom.Style;
import com.openwaf.client.event.dom.ClickEvent;
import com.openwaf.client.event.dom.ClickHandler;
import com.openwaf.client.ui.UIControl;
import com.openwaf.client.ui.annotation.ViewElement;
import com.openwaf.client.ui.annotation.ViewMethod;
import com.openwaf.client.ui.annotation.ViewProperty;
import java.util.ArrayList;
import java.util.Iterator;

public class ExpandPanel extends UIControl implements Iterable{
    private ArrayList elements;
    private boolean expanded;
    @ViewElement
    private DivElement divTitle;
    @ViewElement
    private DivElement divIcon;
    @ViewElement
    private DivElement divTitleText;
    @ViewElement
	private DivElement divContent;

    private static String CSS_ICON_COLLAPSED="waf-panel-expand-icon-collapsed";
    private static String CSS_ICON_EXPANDED="waf-panel-expand-icon-expanded";
    
    private String cssIconCollapsed=CSS_ICON_COLLAPSED;
    private String cssIconExpanded=CSS_ICON_EXPANDED;

    @ViewProperty
    public void setCssContent(String name){
        divContent.setCssClassName(name);
    }
    public String getCssContent(){
        return divContent.getCssClassName();
    }
    @ViewProperty
    public void setCssTitleText(String name){
        divTitleText.setCssClassName(name);
    }
    public String getCssTitleText(){
        return divTitleText.getCssClassName();
    }
    @ViewProperty
    public void setCssTitle(String name){
        divTitle.setCssClassName(name);
    }
    public String getCssTitle(){
        return divTitle.getCssClassName();
    }

    @ViewProperty
    public void setCssIconCollapsed(String name){
        cssIconCollapsed=name;
        if(!expanded)
            divIcon.setCssClassName(cssIconCollapsed);
    }
    public String getCssIconCollapsed(){
        return cssIconCollapsed;
    }

    @ViewProperty
    public void setCssIconExpanded(String name){
        cssIconExpanded=name;
        if(expanded)
            divIcon.setCssClassName(cssIconExpanded);
    }
    public String getCssIconExpanded(){
        return cssIconExpanded;
    }

    public ExpandPanel(){
        expanded=false;
        elements=new ArrayList();
        divTitle.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                if(expanded){
                    collapse();
                }else{
                    expand();
                }
            }
        });
    }
    public ExpandPanel(String text){
        this();
        setTitle(text);
    }
    @Override
    @ViewProperty
    public final void setTitle(String text){
        divTitleText.setInnerHTML(text);
    }
    @Override
    public String getTitle(){
       return divTitleText.getInnerHTML();
    }

    @ViewMethod
    public void setTitle(Element element){
        divTitleText.clearChilds();
        divTitleText.appendChild(element);
    }
    @ViewMethod
    public void addElement(Element element){
        elements.add(element);
        divContent.appendChild(element);        
    }
    public void addElement(int index,Element element){
        if(index<0 || index>=elements.size()){
            throw new IndexOutOfBoundsException("Index:"+index+",Size:"+elements.size());
        }
        Node ex=divContent.getChildAt(index);
        elements.add(index, element);
        divContent.insertBefore(element, ex);
    }
    @ViewMethod
    public void addElement(UIControl control){
        elements.add(control);
        divContent.appendChild(control.getElement());
    }
    public void addElement(int index,UIControl control){
        if(index<0 || index>=elements.size()){
            throw new IndexOutOfBoundsException("Index:"+index+",Size:"+elements.size());
        }
        Node ex=divContent.getChildAt(index);
        elements.add(index, control);
        divContent.insertBefore(control.getElement(), ex);
    }
     public boolean removeElement(UIControl control){
        int index=elements.indexOf(control);
        if(index>=0){
            elements.remove(control);
            divContent.removeChild(control.getElement());
            return true;
        }
        return false;
    }
    public boolean removeElement(Element element){
        int index=elements.indexOf(element);
        if(index>=0){
            elements.remove(element);
            divContent.removeChild(element);
            return true;
        }
        return false;
    }
    public boolean removeElement(int index){
        if(index<0 || index>=elements.size()){
            throw new IndexOutOfBoundsException("Index:"+index+",Size:"+elements.size());
        }        
        divContent.removeChild(divContent.getChildAt(index));
        elements.remove(index);        
        return true;        
    }
    public void expand(){
        if(!expanded){
            divContent.getStyle().setDisplay("");
            divIcon.setCssClassName(cssIconExpanded);
            expanded=true;
        }
    }
    public void collapse(){
        if(expanded){
            divContent.getStyle().setDisplay("none");
            divIcon.setCssClassName(cssIconCollapsed);
            expanded=false;
        }
    }
    public boolean isExpanded(){
        return expanded;
    }
    public boolean isCollapsed(){
        return !expanded;
    }

    
    public Iterator iterator(){
        return elements.iterator();
    }

}
